package com.flashphoner.api
{	
	
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	import com.flashphoner.Logger;
	import com.flashphoner.api.data.Call;
	import com.flashphoner.api.data.ErrorCodes;
	import com.flashphoner.api.data.ModelLocator;
	import com.flashphoner.api.data.PhoneConfig;

	public class PhoneCallback
	{	
		private var flash_API:Flash_API;
		public function PhoneCallback(flash_api:Flash_API)
		{
			this.flash_API = flash_api;
		}
		
		public function notifyBalance(balance:Number,_sipObject:Object):void{
			flash_API.apiNotify.notifyBalance(balance,_sipObject);

		}	
		
		public function getVersion(version:String):void{
			PhoneConfig.VERSION_OF_SERVER = version;
		}
		
		public function getUserData(resObj:Object):void{
			var modelLocator:ModelLocator = flash_API.modelLocator;
			modelLocator.login = resObj.login;
			modelLocator.pwd = resObj.password;
			modelLocator.sipProviderAddress = resObj.sipProviderAddress;
			modelLocator.sipProviderPort = resObj.sipProviderPort;
			
		}
		
		public function fail(errorCode:String,_sipObject:Object):void{
			Logger.info("PhoneCallback.fail() "+errorCode);
			flash_API.apiNotify.notifyError(errorCode,_sipObject);
		}
		
		public function close():void{
			
		}
		
		public function registered(_sipObject:Object):void{
			Logger.info("PhoneCallback.registered()");
			flash_API.apiNotify.notifyRegistered(_sipObject);
			CairngormEventDispatcher.getInstance().dispatchEvent(new MainEvent(MainEvent.REGISTERED,flash_API));
		}
		
		public function ring(_call:Object,_sipObject:Object):void{
			try{
				Logger.info("PhoneCallback.ring()");
				var call:Call = process(_call);
				flash_API.apiNotify.notify(call.state,_sipObject);
				if (!call.incoming){
					CairngormEventDispatcher.getInstance().dispatchEvent(new MainEvent(MainEvent.OUT,flash_API,call));
				} else {
					flash_API.apiNotify.notifyCall(call.caller,call.visibleNameCaller,_sipObject);
					CairngormEventDispatcher.getInstance().dispatchEvent(new MainEvent(MainEvent.IN,flash_API,call));
				}
			}catch(error:Error){
			}				
		}
		
		public function sessionProgress(_call:Object,_sipObject:Object):void{
			try{
				var call:Call = process(_call);
				CairngormEventDispatcher.getInstance().dispatchEvent(new MainEvent(MainEvent.SESSION_PROGRESS,flash_API,call));
			}catch(error:Error){
			}
		}
		
		public function talk(_call:Object,_sipObject:Object):void{
			try{
				var call:Call = process(_call);
				flash_API.apiNotify.notify(call.state,_sipObject);
				CairngormEventDispatcher.getInstance().dispatchEvent(new MainEvent(MainEvent.TALK,flash_API,call));
			}catch(error:Error){
			}
		}
		
		public function busy(_call:Object,_sipObject:Object):void{
			try{
				var call:Call = process(_call);
				flash_API.apiNotify.notify(call.state,_sipObject);
				CairngormEventDispatcher.getInstance().dispatchEvent(new MainEvent(MainEvent.BUSY,flash_API,call));
			}catch(error:Error){
			}				
		}
		
		public function finish(_call:Object,_sipObject:Object):void{
			try{
				var call:Call = process(_call);
				flash_API.apiNotify.notify(call.state,_sipObject);
				CairngormEventDispatcher.getInstance().dispatchEvent(new MainEvent(MainEvent.FINISH,flash_API,call));
			}catch(error:Error){
			}			
		}	
		
		public function process(_call:Object):Call{
			Logger.info("PhoneCallback.process() id="+_call.id+" state="+_call.state+" callee="+_call.callee);
			var call:Call = flash_API.currentCall;
			
			if (call==null){
				call = new Call();
				call.id = _call.id;	
				call.incoming=true;
				call.visibleNameCallee = flash_API.modelLocator.visibleName;
			}else if (call.id != _call.id){
				throw new Error();
			}
			if (_call.isVideoCall == 'true'){
				call.isVideoCall = true;
			}else{
				call.isVideoCall = false;
			} 
			call.callee = _call.callee;
			call.caller = _call.caller;
			call.visibleNameCaller = _call.visibleNameCaller;
			call.state = _call.state;
			call.sip_state = _call.sip_state;
			
			flash_API.currentCall = call;			
			Logger.info("PhoneCallback.process() complete id="+call.id+" state="+call.state+" callee="+call.callee);
			return call;
		}
		
	}
}